package com.tsingtree.modular.service.impl;

import com.tsingtree.modular.entity.NanSellerDevice;
import com.tsingtree.modular.entity.NanSellerTag;
import com.tsingtree.modular.entity.NanUser;
import com.tsingtree.modular.mapper.NanSellerTagMapper;
import com.tsingtree.modular.model.params.NanSellerTagParam;
import com.tsingtree.modular.model.result.NanSellerTagResult;
import com.tsingtree.modular.service.NanSellerDeviceService;
import  com.tsingtree.modular.service.NanSellerTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsingtree.modular.service.NanUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.roses.core.util.ToolUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;


/**
 * <p>
 * 运营组用户分组 服务实现类
 * </p>
 *
 * @author jagger
 * @since 2024-07-09
 */
@Service
public class NanSellerTagServiceImpl extends ServiceImpl<NanSellerTagMapper, NanSellerTag> implements NanSellerTagService {

    @Autowired
    private NanUserService nanUserService;
    @Autowired
    private NanSellerDeviceService nanSellerDeviceService;

    @Override
    public void add(NanSellerTagParam param){
        NanSellerTag entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NanSellerTagParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NanSellerTagParam param){
        NanSellerTag oldEntity = getOldEntity(param);
        NanSellerTag newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NanSellerTagResult findBySpec(NanSellerTagParam param){
        return null;
    }

    @Override
    public List<NanSellerTagResult> findListBySpec(NanSellerTagParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NanSellerTagParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(NanSellerTag byId, List<NanUser> nanUsers, List<NanSellerDevice> nanSellerDevices) {
        if(!this.updateById(byId)){
            throw new RuntimeException();
        }
        if(!CollectionUtils.isEmpty(nanUsers)){
            nanUserService.updateBatchById(nanUsers);
        }
        if(!CollectionUtils.isEmpty(nanSellerDevices)){
            nanSellerDeviceService.updateBatchById(nanSellerDevices);
        }
    }

    private Serializable getKey(NanSellerTagParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NanSellerTag getOldEntity(NanSellerTagParam param) {
        return this.getById(getKey(param));
    }

    private NanSellerTag getEntity(NanSellerTagParam param) {
        NanSellerTag entity = new NanSellerTag();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
